package tech.wetech.admin.modules.system.web;

import com.github.pagehelper.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tech.wetech.admin.core.annotation.SystemLog;
import tech.wetech.admin.core.utils.Result;
import tech.wetech.admin.modules.base.query.PageQuery;
import tech.wetech.admin.modules.base.web.BaseCrudController;
import tech.wetech.admin.modules.system.enums.MachType;
import tech.wetech.admin.modules.system.enums.MachXFType;
import tech.wetech.admin.modules.system.po.Organization;
import tech.wetech.admin.modules.system.po.SysMachInfo;
import tech.wetech.admin.modules.system.service.OrganizationService;
import tech.wetech.admin.modules.system.service.SysMachInfoService;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author lu
 * @description
 * @date 2019年08月02日 16:02
 */
@Controller
@RequestMapping("/machinfo")
public class SysMachInfoController extends BaseCrudController<SysMachInfo> {

    @Autowired
    private SysMachInfoService sysMachInfoService;

    @Autowired
    private OrganizationService organizationService;

    @ModelAttribute("machXFType")//
    public MachXFType[] machXFType() {
        return MachXFType.values();
    }

    @ModelAttribute("machType")//
    public MachType[] machType() {
        return MachType.values();
    }

    @GetMapping
    public String company(Model model) {
        List<Organization> list=organizationService.queryAll();
        model.addAttribute("lists", list);
        return "system/sysmachInfo";
    }

    @GetMapping("/selectlist")
    @ApiOperation("分页查询数据")
    @ResponseBody
    public Result queryList(SysMachInfo sysMachInfo, PageQuery pageQuery) {
        List<SysMachInfo> list = sysMachInfoService.querySelectList(sysMachInfo, pageQuery);
        return Result.success(list)
                .addExtraIfTrue(pageQuery.isCountSql(), "total", ((Page) list).getTotal());
    }

    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions("machinfo:create")
    @SystemLog("机器信息新增")
    @Override
    public Result create(SysMachInfo sysMachInfo) {
        sysMachInfoService.createSysMachInfo(sysMachInfo);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("machinfo:update")
    @SystemLog("机器信息修改")
    @Override
    public Result update(SysMachInfo sysMachInfo) {
        sysMachInfoService.updateSysMachInfo(sysMachInfo);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("machinfo:delete")
    @SystemLog("机器信息删除")
    @PostMapping("/delete-batch")
    @Override
    public Result deleteBatchByIds(@NotNull @RequestParam("id") Object[] ids) {
        super.deleteBatchByIds(ids);
        return Result.success();
    }
}
